package com.example.powerprofiletool;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
    private final String TAG = "MainActivity";

    private final String className = "com.android.internal.os.PowerProfile";
    private final String GET_AVERAGE_POWER = "getAveragePower";

    private final String POWER_CPU_AWAKE = "cpu.awake";

    private Class hideClass = null;
    private Object invokeOperation = null;

    private ArrayList<TextView> textViews = new ArrayList<TextView>();

    private final String[] TAGS = { "none", "cpu.idle", "cpu.awake",
            "wifi.scan", "wifi.on", "wifi.active", "gps.on", "bluetooth.on",
            "bluetooth.active", "bluetooth.at", "screen.on", "radio.scanning",
            "radio.active", "screen.full", "dsp.audio", "dsp.video",
            "battery.capacity", "cpu.active", "radio.on", "cpu.speeds", };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findView();
        Log.v(TAG, "onCreate");
        try {
            hideClass = Class.forName(className);
            Constructor[] cons = hideClass.getConstructors();
            invokeOperation = cons[0].newInstance(this);
            // invokeOperation = hideClass.newInstance();
            // Method[] hideMethod = hideClass.getMethods();
            // int i = 0;
            // for (; i < hideMethod.length; i++) {
            // Log.v(TAG, hideMethod[i].getName());
            // }

            setText();

            // Method method = hideClass.getMethod(GET_AVERAGE_POWER,
            // String.class, int.class);

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void findView() {
        textViews.add((TextView) findViewById(R.id.text1));
        textViews.add((TextView) findViewById(R.id.text2));
        textViews.add((TextView) findViewById(R.id.text3));
        textViews.add((TextView) findViewById(R.id.text4));
        textViews.add((TextView) findViewById(R.id.text5));
        textViews.add((TextView) findViewById(R.id.text6));
        textViews.add((TextView) findViewById(R.id.text7));
        textViews.add((TextView) findViewById(R.id.text8));
        textViews.add((TextView) findViewById(R.id.text9));
        textViews.add((TextView) findViewById(R.id.text10));
        textViews.add((TextView) findViewById(R.id.text11));
        textViews.add((TextView) findViewById(R.id.text12));
        textViews.add((TextView) findViewById(R.id.text13));
        textViews.add((TextView) findViewById(R.id.text14));
        textViews.add((TextView) findViewById(R.id.text15));
        textViews.add((TextView) findViewById(R.id.text16));
        textViews.add((TextView) findViewById(R.id.text17));
        textViews.add((TextView) findViewById(R.id.text18));
        textViews.add((TextView) findViewById(R.id.text19));
        textViews.add((TextView) findViewById(R.id.text20));
    }

    private void setText() {

        for (int i = 0; i < TAGS.length; i++) {
            if (i < 17) {
                textViews.get(i).setText(getAveragePower(TAGS[i]));
            } else {
                StringBuilder result = new StringBuilder();
                int j = 0;
                String preResult = "";
                // while (!(getAveragePower(TAGS[i], j)).equals("0")) {
                while (true) {
                    String r = getAveragePower(TAGS[i], j);
                    Log.v(TAG, "preResult===" + preResult);
                    Log.v(TAG, "r===" + r);
                    if (preResult.equals(r)) {
                        Log.v(TAG, "break");
                        break;
                    }
                    result.append(r);
                    preResult = r;
                    j++;
                }
                textViews.get(i).setText(result.toString());
            }
        }
    }

    private String getAveragePower(String tag) {
        Method method;
        String sresult = "";
        try {
            method = hideClass.getMethod(GET_AVERAGE_POWER, String.class);
            Object result = method.invoke(invokeOperation, tag);
            sresult = tag + " = " + result.toString();

        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sresult;
    }

    private String getAveragePower(String tag, int level) {
        Method method;
        String sresult = "";
        try {
            method = hideClass.getMethod(GET_AVERAGE_POWER, String.class,
                    int.class);
            Object result = method.invoke(invokeOperation, tag, level);
            sresult = tag + " = " + result.toString() + "\n";

        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sresult;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
